﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Anthem;
using spUtils;
using spMapping;
using spData.Common;
using spWeb;

namespace demo.Page.RS
{
    public partial class rs_Btsqd : TaskPage
    {
        #region Property
        public List<dc_rs_Btsqd_Details> details
        {
            get { return ViewState["details"] == null ? new List<dc_rs_Btsqd_Details>() : ViewState["details"] as List<dc_rs_Btsqd_Details>; }
            set { ViewState["details"] = value; }
        }
        #endregion

        

        #region Method Init Controls Value & Show/Hide & Enable/Disable & Opinion
        private void InitControls()
        {
            #region Init controls value
        

            Expression exp = new Expression();
            string did = Request.QueryString["DID"];
            exp = string.IsNullOrEmpty(did) ? dc_rs_Btsqd.process == TaskAPI.Process & dc_rs_Btsqd.incident == TaskAPI.Incident : dc_rs_Btsqd.id == did;
            if (TaskAPI.IsInitiate)
            {
                exp &= dc_rs_Btsqd.userName == USER.UserName;
            }

            dc_rs_Btsqd v = exp.ListFirst<dc_rs_Btsqd>();
            if (v != null)
            {
                SetControlsValue(v);
                DepEName.Text = v.Department;
                //DID方式加载
                if (!string.IsNullOrEmpty(did))
                {
                    TaskAPI.LoadByDID(v.Process, v.Incident);
                    Process = v.Process;
                    Incident = v.Incident;
                }
            }
            else
            {
                UserFullName.Text = USER.UserFullName;
                Time.Text = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                Department.Text = USER.Department;
                DepEName.Text = USER.DepEName;
                Tel.Text = USER.EXTEND.CellPhone;
            }

            BindDetails();
            PushKjkm();
            #endregion

            #region Init Show/Hide、Enable/Disable
            bool flg = TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit;
            SetEnable(flg, Sqyy, Kszxsj);
            SetVisible(flg, R_Sqyy, R_Kszxsj);
            #endregion
        }
        #endregion
        #region Method BindDetails
        private void BindDetails()
        {
            Expression exp = (dc_rs_Btsqd_Details.process == TaskAPI.Process & dc_rs_Btsqd_Details.incident == TaskAPI.Incident & dc_rs_Btsqd_Details.status == 1);

            details = exp.List<dc_rs_Btsqd_Details>(new Region(0, 100), dc_rs_Btsqd_Details.id.Asc) as List<dc_rs_Btsqd_Details>;
            if (details.Count == 0 & TaskAPI.IsBeginStep)
            {
                for (int i = 0; i < 3; i++)
                {
                    details.Add(new dc_rs_Btsqd_Details());
                }
            }
            Details.DataSource = details;
            Details.DataBind();
        }

        protected void Details_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //null
        }

        #endregion

        #region Method Data Validation
        private List<string> DataValidation()
        {
            List<Regular> regs = new List<Regular>();
            if (!AssExt.IsConferWithBack && !AssExt.IsForwardWithBack)
            {
                if (TaskAPI.IsBeginStep)
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, Sqyy.Text, "“申请原因”为必填项!"));
                    regs.Add(new Regular(RegularExp.NotEmpty, Kszxsj.Text, "“开始执行时间”为必填项!"));
            
                }
            }
            return Validation(regs);
        }
        #endregion

        #region Method Engine Transfer Value
        private void EngineTransferValue()
        {
            if (TaskAPI.IsBeginStep)
            {
                TaskAPI.Summary = Process;

            }
            TaskAPI["e_Approve"] = AssExt.Assessment.Opinion;
        }

        #endregion

        #region Method Submit
        private bool Submit(bool IsSend)
        {
            #region “开始”步骤
            if (TaskAPI.IsBeginStep)
            {
                dc_rs_Btsqd v = (dc_rs_Btsqd.process == TaskAPI.Process & dc_rs_Btsqd.incident == TaskAPI.Incident
                                & dc_rs_Btsqd.userName == (TaskAPI.IsInitiate ? USER.UserName : TaskAPI.UserName)).ListFirst<dc_rs_Btsqd>();
                bool result = GetControlsValue<dc_rs_Btsqd>(ref v);
                if (result)
                {
                    v.Process = TaskAPI.Process;
                    v.Incident = TaskAPI.Incident;
                    v.UserName = USER.UserName;
                    v.UserFullName = USER.UserFullName;
                    v.Time = System.DateTime.Now;
                    ApplicationNo.Text = GetNewSequence("KCG-CGLXD-" + DateTime.Now.ToString("yyyyMMdd") + "-", "", 3);
                    v.ApplicationNo = ApplicationNo.Text;
                }
                ORM.Save(v);
            }
            #endregion


            if (IsSend)
            {
                EngineTransferValue();
                ORM.Save(AssExt.Assessment);
                TaskAPI.Send();
            }

            if (IsSend && TaskAPI.IsInitiate)
            {
                (dc_rs_Btsqd.process == TaskAPI.Process & dc_rs_Btsqd.incident == 0 & dc_rs_Btsqd.userName == USER.UserName).Edit<dc_rs_Btsqd>(p =>
                {
                    p.Incident = TaskAPI.Incident;

                }
                );
            }

            //退出
            if (IsSend)
            {
                Exit();
            }
            return true;
        }
        #endregion

        #region Event Page
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitControls();
            }
        }
        #endregion

        #region Event Button Click

        #region Event Save As Draft
        protected void SaveAsDraft_Click(object sender, EventArgs e)
        {
            bool success = Submit(false);
            Anthem.Manager.AddScriptForClientSideEval(string.Format("JSInfomation('{0}')", success ? "保存成功!" : "保存失败!"));
        }
        #endregion

        #region Event Send
        protected void Send_Click(object sender, EventArgs e)
        {
            List<string> msg = DataValidation();
            if (msg.Count > 0)
            {
                Anthem.Manager.AddScriptForClientSideEval(Alert(msg));
                return;
            }

            bool success = Submit(true);
            (sender as spWeb.Controls.AssessmentExt).Refresh();
            Anthem.Manager.AddScriptForClientSideEval(string.Format("JSInfomation('{0}');{1}", success ? "提交成功!" : "提交失败!", success ? "window.close();" : ""));
        }
        #endregion

        #region Event Aborted
        protected void Aborted_Click(object sender, EventArgs e)
        {
            (dc_rs_Btsqd.process == TaskAPI.Process & dc_rs_Btsqd.incident == TaskAPI.Incident).Edit<dc_rs_Btsqd>(p => { p.Status = 4; });
        }
        #endregion

        #endregion



        #region Event Row Add, Delete
        protected void AddDelete_Click(object sender, EventArgs e)
        {
            //收集数据
            CollectKjkm();

            if ((sender as Anthem.Button).ID == "detailsDel")
            {
                foreach (RepeaterItem ri in Details.Items)
                {
                    Anthem.CheckBox current = ri.FindControl("Cb_XZ") as Anthem.CheckBox;
                    if (current.Checked)
                    {
                        int index = CommHelper.ConvertToInt(current.Attributes["alt"]);
                        if (index != 0)
                        {
                            for (int i = 0; i < details.Count; i++)
                            {
                                if (details[i].No == index)
                                {
                                    details.RemoveAt(i);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                details.Add(new dc_rs_Btsqd_Details());
            }

            //重新编号、绑定
            for (int i = 0; i < details.Count; i++)
            {
                details[i].No = i;
            }
            Details.DataSource = details;
            Details.DataBind();

            PushKjkm();
        }
        #endregion

        #region Method CollectKjkm, PushKjkm
        protected void CollectKjkm()
        {
            int i = 0;
            foreach (RepeaterItem ri in Details.Items)
            {
                Anthem.TextBox Btr = ri.FindControl("Btr") as Anthem.TextBox;
                Anthem.TextBox Txf = ri.FindControl("Txf") as Anthem.TextBox;
                Anthem.TextBox Ycxzsbt = ri.FindControl("Ycxzsbt") as Anthem.TextBox;
                Anthem.TextBox Wpzsbt = ri.FindControl("Wpzsbt") as Anthem.TextBox;
                Anthem.TextBox Gdjt = ri.FindControl("Gdjt") as Anthem.TextBox;
                Anthem.TextBox Jtjt = ri.FindControl("Jtjt") as Anthem.TextBox;
                Anthem.TextBox Scgybt = ri.FindControl("Scgybt") as Anthem.TextBox;

                details[i].Btr = Btr.Text;
                details[i].Txf = Txf.Text;
                details[i].Ycxzsbt = Ycxzsbt.Text;
                details[i].Wpzsbt = Wpzsbt.Text;
                details[i].Gdjt = Gdjt.Text;
                details[i].Jtjt = Jtjt.Text;
                details[i].Scgybt = Scgybt.Text;
                

                i++;
            }
        }

        protected void PushKjkm()
        {
            int i = 0;
            bool isModifiy = TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit;
            foreach (RepeaterItem ri in Details.Items)
            {
                Anthem.CheckBox No = ri.FindControl("Cb_XZ") as Anthem.CheckBox;
                Anthem.TextBox Btr = ri.FindControl("Btr") as Anthem.TextBox;
                Anthem.TextBox Txf = ri.FindControl("Txf") as Anthem.TextBox;
                Anthem.TextBox Ycxzsbt = ri.FindControl("Ycxzsbt") as Anthem.TextBox;
                Anthem.TextBox Wpzsbt = ri.FindControl("Wpzsbt") as Anthem.TextBox;
                Anthem.TextBox Gdjt = ri.FindControl("Gdjt") as Anthem.TextBox;
                Anthem.TextBox Jtjt = ri.FindControl("Jtjt") as Anthem.TextBox;
                Anthem.TextBox Scgybt = ri.FindControl("Scgybt") as Anthem.TextBox;


                SetEnable(isModifiy, Btr, Txf, Ycxzsbt, Wpzsbt, Gdjt, Jtjt, Scgybt);

                No.Attributes["alt"] = i.ToString();
                i++;
            }
        }
        #endregion

        
    }
}